package com.st.springts.controller;

import com.st.springts.demos.web.Result;
import com.st.springts.entity.Department;
import com.st.springts.entity.DepartmentDTO;
import com.st.springts.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/departments")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/tree")
    public Result getDepartmentTree(){
        long startTime = System.currentTimeMillis();
        List<DepartmentDTO> tree = departmentService.getDepartmentTree();
        long duration = System.currentTimeMillis() - startTime;

        return Result.success("查询公司树结构成功",tree);
    }

@GetMapping("/tree/manual")
    public Result getDepartmentTreeManual(){
    long startTime = System.currentTimeMillis();
    List<DepartmentDTO> tree = departmentService.getDepartmentTreeWithManualCache();
    long duration = System.currentTimeMillis() - startTime;

    return Result.success("查询公司树结构成功",tree);
    }

    //清除缓存
    @PostMapping("/cache/clear")
    public Result clearCache(){
        departmentService.clearCache();
        departmentService.clearManualCache();

        return Result.success("TRUE","清楚缓存成功");
    }

//    @GetMapping("/tree")
//    public Result getDepartmentTree(){
//        List<Department> tree = departmentService.getDepartmentTree();
//        return Result.success("sucess",tree);
//    }
}
